# This Python file uses the following encoding: utf-8

'''
@author: weilan
@date: 2022/10/31

工程介绍：
通过图形视图框架（QGraphicsScene、QGraphicsView和QGraphicsItem三个类）实现了一个简单的画板程序.
画板程序很简单，只实现了图片的旋转、缩放和移动功能.

环境： VScode 、 pyside6、 Python3.10

参考：
[1]自定义QGraphicsItem实现平移、改变尺寸和旋转(https://blog.csdn.net/douzhq/article/details/105017924) 

[2]向量旋转公式(https://blog.csdn.net/zhinanpolang/article/details/82912325)

'''

import sys
from PySide6 import QtCore, QtWidgets, QtGui
from PySide6.QtWidgets import QMainWindow, QGraphicsScene, QGridLayout, QGraphicsItem, QButtonGroup, \
      QPushButton
from PySide6.QtGui import Qt
from ui_form import Ui_MainWindow

from my_graphics_view import MyGraphicsView
from my_graphics_scene import MyGraphicsScene
from item.item_type import itemType

class MainWindow(QMainWindow):

    def __init__(self, parent = None) :
        super().__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.__scene = MyGraphicsScene(self)    #新建场景
        self.__view = MyGraphicsView(self.__scene, self.ui.widget) #视图
        self.__layout = QGridLayout(self.ui.widget)
        self.__layout.addWidget(self.__view)   #将视图布局到控件 widget 中
        self.ui.widget.setLayout(self.__layout)
        self.ui.btnRectangle.raise_()   # 将按钮放在最上面
        self.__buttonGroup = QButtonGroup(self)
        self.__buttonGroup.addButton(self.ui.btnRectangle, itemType.RECTANGLE.value)
        self.__buttonGroup.idClicked.connect(self.btnsClickSlot)

    @QtCore.Slot(int)
    def btnsClickSlot(self, btnID):
        if btnID == itemType.RECTANGLE.value :   #矩形
            self.__scene.setNewItem(itemType.RECTANGLE)
        else :
            self.__scene.setNewItem(None)
        

'''
从这里执行
'''
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.setWindowTitle("Draw")
    mainWindow.show()
    app.exit(app.exec())
